package cn.itcast.dao;

import cn.itcast.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;


/**
 * Author: Saka
 * Date: 2019/1/10
 * Time: 13:08
 */
@Component //写一个假的dao
public class FakeDao {

    @Autowired
    private RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "queryUserByIdFb")
    public User queryUserById(Long id) {

        String baseUrl = "http://user-service/user/";
        long startTime = System.currentTimeMillis();


        User user = this.restTemplate.getForObject(baseUrl + id, User.class);

        long endTime = System.currentTimeMillis();

        System.out.println("本次查询"+id+"耗时:"+(endTime-startTime));

        return user;
    }

    public User queryUserByIdFb(Long id) {
        User user = new User();
        user.setId(id);
        user.setName("查询用户信息异常");
        return user;
    }
}
